package com.leif.eventbusdemo.activity;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.leif.eventbusdemo.R;
import com.leif.eventbusdemo.pojo.MessageEvent;
import com.leif.eventbusdemo.pojo.OtherEvent;

import de.greenrobot.event.EventBus;


public class MainActivity extends ActionBarActivity {

    private Button btnMessage, btnOther;
    private TextView tvMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnMessage = (Button) findViewById(R.id.btnMessageEvent);
        btnOther = (Button) findViewById(R.id.btnOtherEvent);
        tvMessage = (TextView) findViewById(R.id.tvMessage);

        btnMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MessageEvent("Hello Message Event!"));
            }
        });

        btnOther.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new OtherEvent("Hello Other Event!"));
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }

    // This method will be called when a MessageEvent is posted,  // Called in the same thread (default)
    public void onEvent(MessageEvent messageEvent) {
        Toast.makeText(this, messageEvent.message, Toast.LENGTH_SHORT).show();
    }


    // This method will be called when a OtherEvent is posted
    public void onEvent(OtherEvent messageEvent) {
        Toast.makeText(this, messageEvent.message, Toast.LENGTH_SHORT).show();
    }

    // Called in Android UI's main thread
    public void onEventMainThread(MessageEvent event) {
        tvMessage.setText(event.message);
    }

    // Called in the background thread
    public void onEventBackgroundThread(MessageEvent event){
        //saveToDisk(event.message);
    }

    // Called in a separate thread
    public void onEventAsync(MessageEvent event){
        //backend.send(event.message);
    }

}